﻿using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using VPLink2013.Framework;
using VPLink2013.Framework.Extensions;
using VPLink2013.Framework.Constants;
using System;

namespace VPLink2013.Webcontrols
{
    public class OutilsPratiques: WebcontrolVPLink
    {
        protected override void Render(HtmlTextWriter writer)
        {
            Template = "webcontrol OutilsPratiques";
            try
            {
                if(!string.IsNullOrEmpty(siteUrl))
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.RootWeb)
                        {
                            bool isFrenchVersion = true;
                            isFrenchVersion = UtilisateurCrt.Pays.Equals("France", StringComparison.CurrentCultureIgnoreCase);
                            if (isFrenchVersion) 
                            { 
                                SPList lst = web.Lists.TryGetList(ListTitles.OutilsPratiquesTitle);
                                if (lst != null)
                                {
                                    ProcessOutilsPratiques(lst);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                ContextTrace.AddTrace("webcontrol OutilsPratiques.Erreur.Render : " + exc.Message + "/" + exc.StackTrace);
            }
            base.Render(writer);
        }

        private void ProcessOutilsPratiques(SPList lst)
        {
            SPQuery spQuery = new SPQuery();
            StringBuilder sb = new StringBuilder();
            sb.Append("<Where>");
            sb.Append("<Eq><FieldRef Name='Visible'/><Value Type='Text'>Oui</Value></Eq>");
            sb.Append("</Where>");
            sb.Append("<OrderBy><FieldRef Name='Ordre' Ascending='TRUE'/><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>");
            string Query = sb.ToString();
            spQuery.Query = Query;
            ContextTrace.AddTrace("spQuery.Query : " + spQuery.Query);

            SPListItemCollection items = lst.GetItems(spQuery);
            //int intWidth = 100 / items.Count;

            sbHtml.AppendLine("<header>");
            sbHtml.AppendLine("<h2><span class=\"highlight hometitle\">SERVICES PRATIQUES</span></h2>");
            sbHtml.AppendLine("</header>");
            sbHtml.AppendLine("<div id=\"pictos_tools\">");            
            sbHtml.AppendLine("<div class=\"bg_middle-toolsBar\"></div>");
            sbHtml.AppendLine("<ul>");

            sb = new StringBuilder();
            sb.Append("<style type=\"text/css\">");
            int iPosition = 1;
            foreach (SPListItem item in items)
            {
                try
                {
                    iPosition = CreateOutilParatiqueItem(sb, iPosition, item);
                }
                catch (Exception ex)
                {
                    ContextTrace.AddTrace("Exception lors de la creation d'un item outil pratique " + ex.Message);
                    ContextTrace.AddTrace(ex.StackTrace);
                }
            }
            sb.Append("</style>");
            sbHtml.AppendLine("</ul>");
            sbHtml.AppendLine("</div>");

            //CSS pour les images
            sbHtml.Insert(0, sb.ToString());


//#pictos_tools ul li:nth-child(1) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_transport.png) no-repeat;
	
//}
//#pictos_tools ul li:nth-child(2) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_restauration.png) no-repeat;
//}
//#pictos_tools ul li:nth-child(3) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_vpshop.png) no-repeat;
//}
//#pictos_tools ul li:nth-child(4) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_conciergerie.png) no-repeat;
//}
//#pictos_tools ul li:nth-child(5) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_annonce.png) no-repeat;
//}
//#pictos_tools ul li:nth-child(6) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_sport.png) no-repeat;
//}
//#pictos_tools ul li:nth-child(7) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_ce.png) no-repeat;
//}
//#pictos_tools ul li:nth-child(8) a{
//    background:url(/_layouts/15/VPLink2013/Images/tool_urgence.png) no-repeat;
//}

        }

        private int CreateOutilParatiqueItem(StringBuilder sb, int iPosition, SPListItem item)
        {
            ContextTrace.AddTrace("-------------------------------");
            string strLinkURL = item.GetItemListFieldValueAsUrlValue(Fields.UrlNewsField);
            string strTexteURL = item.GetItemListFieldValueAsUrlDescription(Fields.UrlNewsField);
            string strTextePiwik = strTexteURL.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");

            ContextTrace.AddTrace("strTexteURL : " + strTexteURL);
            ContextTrace.AddTrace("strLinkURL : " + strLinkURL);

            string strLinkImage = item.GetItemListFieldValueAsUrlValue(Fields.ImageField);
            string strTexteImage = item.GetItemListFieldValueAsUrlDescription(Fields.ImageField);

            ContextTrace.AddTrace("strLinkImage : " + strLinkImage);
            ContextTrace.AddTrace("strTexteImage : " + strTexteImage);

            string strURL = "<a href=\"" + strLinkURL + "\" onclick=\"javascript:_paq.push(['trackPageView', 'SERVICES PRATIQUES/" + strTextePiwik + "']);\"><span>" + strTexteURL + "</span></a>";
            ContextTrace.AddTrace("strURL : " + strURL);

            sbHtml.AppendLine("<li id='pictos_tool" + iPosition.ToString() + "'>");
            sbHtml.AppendLine(strURL);
            sbHtml.AppendLine("</li>");

            sb.AppendLine("#pictos_tool" + iPosition.ToString() + " a{");
            sb.AppendLine("background:url(" + strLinkImage + ") no-repeat;}");

            sb.AppendLine("#pictos_tool" + iPosition.ToString() + " a:hover{");
            sb.AppendLine("background-position: left bottom;}");

            iPosition++;
            return iPosition;
        }

    }
}
